import apis from "../../http/apis";
import axios from "../../http/axios";
export default {
  namespaced: true,
  state: {
    type: true,
    adminsData: [],
    //总页数
    pages: 0,
    //总条数
    total: 0,
    //每页显示的条数
    pageSize: 3,
    //当前页
    currentPage: 1,
  },
  mutations: {
    setGetAdmins(state, data) {
      state.adminsData = data.admins;
      state.total = data.total;
      state.pages = data.pages;
    },
    setPageSize(state, pageSize) {
      state.pageSize = pageSize;
    },
    setCurrentPage(state, currentPage) {
      state.currentPage = currentPage;
    },
  },
  actions: {
    //获取
    async getAdmins(context) {
      console.log(context);
      const { data } = await // console.log(context, 111);
      axios({
        url: "/admins/getAdmins",
        method: "get",
        params: {
          pageSize: context.state.pageSize,
          currentPage: context.state.currentPage,
        },
      });
      console.log(data, 2222);
      context.commit("setGetAdmins", data.data);
    },

        //新增
        async addAdmins(context){

            const {data}= await apis.admins.addAdmins();
            context.commit('setGetAdmins', data.data)
           },
        //修改管理员信息
        async changeAdmins(context,admin){
            try {
                const {data}=await apis.admins.changeAdmins(admin)
                if(data.status){
                    Message.success('修改成功')
                }else{
                    Message.error("修改失败")
                }
            } catch (error) {
                console.log(error);
            }
        },
        async changeAdminsCode(context, params) {
            const {data} = await apis.admins.changeAdminsCode(params);
            if(data.status) {
                alert('修改成功');
                context.dispatch('getAdmins')
            }
        }
    }
}
